package common.core.domain;

import common.core.constans.StatusCode;
import lombok.Data;

@Data
public class R<T> {
    private int code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, StatusCode.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, StatusCode.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, StatusCode.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, StatusCode.FAIL, null);
    }

    public static <T> R<T> failed(int code) {
        return restResult(null, code, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, StatusCode.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, StatusCode.FAIL, null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, StatusCode.FAIL, msg);
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
